package emulator.unittest.hardware.nmos6502.commands;

import static org.junit.Assert.*;

import org.junit.Test;

import emulator.EmulatorException;
import emulator.hardware.HwByte;
import emulator.hardware.nmos6502.Cpu6502;
import emulator.hardware.nmos6502.Flags6502;
import emulator.hardware.nmos6502.commands.ROL;
import emulator.unittest.hardware.nmos6502.MockOperand;

public class ROLTest {

	@Test
	public void testRotatePositive() throws EmulatorException {
		executeAndCheckROL(0, 0x08, 0, 0x10);
	}

	@Test
	public void testRotateNegative() throws EmulatorException {
		executeAndCheckROL(Flags6502.FLAG_CARRY, 0x40, Flags6502.FLAG_NEGATIVE, 0x81);
	}

	@Test
	public void testRotateNegativeCarry() throws EmulatorException {
		executeAndCheckROL(Flags6502.FLAG_CARRY, 0xC0, Flags6502.FLAG_NEGATIVE|Flags6502.FLAG_CARRY, 0x81);
	}

	@Test
	public void testRotateZero() throws EmulatorException {
		executeAndCheckROL(0, 0x80, Flags6502.FLAG_ZERO|Flags6502.FLAG_CARRY, 0);
	}

	private void executeAndCheckROL(int flags, int val, int expected_flags, int expected_val)
			throws EmulatorException {
		Cpu6502 cpu = new Cpu6502();
		MockOperand operand = new MockOperand(val);
		cpu.setFlags(new Flags6502(flags));
		new ROL().execute(cpu, operand);
		assertEquals(new HwByte(expected_val), operand.getByte());
		assertEquals(new Flags6502(expected_flags), cpu.getFlags());
	}
}
